package thread;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import util.Tools;

/**
 * 文件内容处理
 * 
 * @author wangpeihu
 * 
 */
public class StatisNameHandler implements Runnable {
    private CountDownLatch threadsSignal;
    public List<String> nameList;

    StatisNameHandler(List<String> nameList, CountDownLatch threadsSignal) {
        this.nameList = nameList;
        this.threadsSignal = threadsSignal;
    }

    public void run() {
        Map<String, Integer> userName = new HashMap<String, Integer>();
        userName.put("宝玉", 0);
        userName.put("黛玉", 0);
        userName.put("宝钗", 0);
        userName.put("凤姐", 0);
        userName.put("元春", 0);
        userName.put("迎春", 0);
        userName.put("探春", 0);
        userName.put("惜春", 0);
        userName.put("史湘云", 0);
        userName.put("秦可卿", 0);
        for (int i = 0; i < nameList.size(); i++) {
            Iterator<String> keyOfName = userName.keySet().iterator();
            while (keyOfName.hasNext()) {
                String name = keyOfName.next();
                Integer cnt = Tools.countMatches(nameList.get(i), name);
                if (cnt > 0) {
                    userName.put(name, userName.get(name) + cnt);
                }
            }
        }

        Iterator<String> keyOfName = userName.keySet().iterator();
        while (keyOfName.hasNext()) {
            String name = keyOfName.next();
            int vlaue = userName.get(name);
            Tools.userName.put(name, Tools.userName.get(name) + vlaue);
        }
        threadsSignal.countDown();

    }
}
